﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint.LookupPicker (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using Res = Sapiens.at.SharePoint.Fields.Resources.Sapiens_at_SharePoint_LookupPicker;
using Microsoft.SharePoint;
using Sapiens.at.SharePoint.Fields.WebParts.LookupFilter;

namespace Sapiens.at.SharePoint.Fields
{
    public class LookupFilterEditorPart:EditorPart
    {
        protected DropDownList Webs { get; set; }
        protected DropDownList Lists { get; set; }
        protected DropDownList MainDisplayField { get; set; }
        protected CheckBoxList AdditionalSearchColumns { get; set; }
        protected CheckBoxList AdditionalDisplayColumns { get; set; }
        protected CheckBoxList ContentTypes { get; set; }
        protected CheckBox AllowShowAll { get; set; }
        protected DropDownList SearchScope { get; set; }

        protected override void CreateChildControls()
        {
            Webs = new DropDownList
            {
                CssClass = "UserInput",
                ID = ID + "Webs",
                AutoPostBack = true,
            };
            Webs.SelectedIndexChanged += new EventHandler(Webs_SelectedIndexChanged);

            Lists = new DropDownList
            {
                CssClass = "UserInput",
                ID = ID + "Lists",
                AutoPostBack = true,
            };
            Lists.SelectedIndexChanged += new EventHandler(Lists_SelectedIndexChanged);

            MainDisplayField = new DropDownList
            {
                CssClass = "UserInput",
                ID = ID + "MainDisplayField",
            };

            AdditionalDisplayColumns = new CheckBoxList
            {
                CssClass = "UserInput",
                ID = ID + "AdditionalDisplayColumns"
            };

            AdditionalSearchColumns = new CheckBoxList
            {
                CssClass = "UserInput",
                ID = ID + "AdditionalSearchColumns"
            };

            ContentTypes = new CheckBoxList
            {
                CssClass = "UserInput",
                ID = ID + "ContentTypes"
            };

            AllowShowAll = new CheckBox
            {
                CssClass = "UserInput",
                ID = ID + "AllowShowAll"
            };

            SearchScope = new DropDownList
            {
                CssClass = "UserInput",
                ID =ID + "SearchScope"
            };
            LookupEditorDataCollector.FillSearchScope(SearchScope);

            LookupEditorDataCollector.FillWebsList(Webs);
            
            var panel = new UpdatePanel { ID = ID + "UpdatePanel" };
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"<table cellspacing=""0"" border=""0"" style=""border-width:0px;width:100%; border-collapse:collapse;""><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format(@"{0}:<br/>", Res.Web)));
            panel.ContentTemplateContainer.Controls.Add(Webs);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.List)));
            panel.ContentTemplateContainer.Controls.Add(Lists);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.MainDisplayField)));
            panel.ContentTemplateContainer.Controls.Add(MainDisplayField);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.AdditionalDisplayFields)));
            panel.ContentTemplateContainer.Controls.Add(AdditionalDisplayColumns);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.AdditionalSearchFields)));
            panel.ContentTemplateContainer.Controls.Add(AdditionalSearchColumns);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.LimitContentTypes)));
            panel.ContentTemplateContainer.Controls.Add(ContentTypes);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.AllowShowAll)));
            panel.ContentTemplateContainer.Controls.Add(AllowShowAll);
            panel.ContentTemplateContainer.Controls.Add(
                new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr><tr><td><div class=""UserSectionHead"">"));
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(string.Format("{0}:<br/>", Res.SearchScope)));
            panel.ContentTemplateContainer.Controls.Add(SearchScope);
            panel.ContentTemplateContainer.Controls.Add(new LiteralControl(@"</div></td></tr><tr><td><div style=""width:100%"" class=""UserDottedLine""></div></td></tr></table>"));

            Controls.Add(panel);
            base.CreateChildControls();
        }

        void Lists_SelectedIndexChanged(object sender, EventArgs e)
        {
            LookupEditorDataCollector.FillWithList(MainDisplayField, AdditionalSearchColumns, AdditionalDisplayColumns, ContentTypes, Webs.SelectedValue, Lists.SelectedValue);
        }

        void Webs_SelectedIndexChanged(object sender, EventArgs e)
        {
            LookupEditorDataCollector.FillListsList(Lists, Webs.SelectedValue);
        }

        public override bool ApplyChanges()
        {
            var part = (LookupFilter)WebPartToEdit;
            // apply values back to web part from editor part
            part.WebID = new Guid(Webs.SelectedValue);
            part.ListID = new Guid(Lists.SelectedValue);
            part.LookupField = MainDisplayField.SelectedValue;
            part.AdditionalDisplayFields = AdditionalDisplayColumns.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToArray();
            part.AdditionalSearchFields = AdditionalSearchColumns.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToArray();
            part.AllowShowAll = AllowShowAll.Checked;
            part.ContentTypes = ContentTypes.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToArray();
            part.SearchScope = SearchScope.SelectedValue;
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            var part = (LookupFilter)WebPartToEdit;
            // sync values from web part to editor part
            Webs.SelectedValue = part.WebID.ToString();
            LookupEditorDataCollector.FillListsList(Lists, Webs.SelectedValue);
            Lists.SelectedValue = part.ListID.ToString();
            LookupEditorDataCollector.FillWithList(MainDisplayField, AdditionalSearchColumns, AdditionalDisplayColumns, ContentTypes, Webs.SelectedValue, Lists.SelectedValue);
            MainDisplayField.SelectedValue = part.LookupField;
            if (part.AdditionalDisplayFields != null)
            {
                foreach (ListItem item in AdditionalDisplayColumns.Items)
                {
                    if (part.AdditionalDisplayFields.Contains(item.Value)) { item.Selected = true; }
                }
            }
            if (part.AdditionalSearchFields != null)
            {
                foreach (ListItem item in AdditionalSearchColumns.Items)
                {
                    if (part.AdditionalSearchFields.Contains(item.Value)) { item.Selected = true; }
                }
            }
            AllowShowAll.Checked = part.AllowShowAll;
            if (part.ContentTypes != null)
            {
                foreach (ListItem item in ContentTypes.Items)
                {
                    if (part.ContentTypes.Contains(item.Value)) { item.Selected = true; }
                }
            }
            SearchScope.SelectedValue = part.SearchScope;
        }
    }
}
